from django.db import models
import datetime


class EmaliModel(models.Model):
    address = models.CharField(verbose_name="发送地址", max_length=100)
    name = models.CharField(verbose_name="抄送人", max_length=100)
    title = models.CharField(verbose_name="发件标题", max_length=100)
    content = models.TextField(verbose_name="发件内容", max_length=500)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "emali_tb"
        verbose_name = "邮件表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 部门表（1）
class DepartmentModel(models.Model):
    # 备注
    name = models.CharField(verbose_name="部门名称", max_length=25)
    department_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    remark = models.CharField(verbose_name="备注", null=True, max_length=100, blank=True)

    level = models.ForeignKey("self",verbose_name="部门层级", null=True, blank=True, on_delete=models.CASCADE)

    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "department_tb"
        verbose_name = "部门表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 职级表（2）
class RankModel(models.Model):
    # 备注 note
    name = models.CharField(verbose_name="职级名称", max_length=25)
    rank_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    remark = models.CharField(verbose_name="备注", null=True, max_length=100, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "rank_tb"
        verbose_name = "职级表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 角色表（3）
class RoleModel(models.Model):
    # 角色权限 ， 备注
    rolename = models.CharField(verbose_name="角色名称", max_length=25)
    role_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    remark = models.CharField(verbose_name="备注", null=True, max_length=100, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "role_tb"
        verbose_name = "角色表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.rolename


# 权限表（4）
class PermissionModel(models.Model):
    permission_name = models.CharField(verbose_name="权限名称", max_length=25)
    path = models.CharField(verbose_name="权限", max_length=50)
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="角色名称")
    permission_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "permission_tb"
        verbose_name = "权限表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.permission_name


# 职位表（5）
class PositionModel(models.Model):
    name = models.CharField(verbose_name="职位名称", max_length=100)
    # position_name = models.CharField(verbose_name="权限名称", max_length=25)
    rank = models.ForeignKey(RankModel, on_delete=models.CASCADE, verbose_name="所属职级")
    department = models.ForeignKey(DepartmentModel, on_delete=models.CASCADE, verbose_name="所属部门")
    position_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "position_tb"
        verbose_name = "职位表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 职位角色中间表（6）
class PositionRoleModel(models.Model):
    position = models.ForeignKey(PositionModel, on_delete=models.CASCADE, verbose_name="职位")
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="角色")
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "position_role_tb"
        verbose_name = "权限中间表_角色"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s：职位 %s：角色" % (self.position.name, self.role.rolename)


# 用户表（7）
class UserModel(models.Model):
    # 账号account
    STATE_OPTIONS = (
        (0, "实习生"), (1, "代理离职"), (2, "离职"), (3, "入职"), (4, "待入职"), (5, "带转正")
    )
    name = models.CharField(verbose_name="用户名", max_length=25)
    account = models.CharField(verbose_name="账号", max_length=50)
    password = models.CharField(verbose_name="用户密码", max_length=16)
    mobile = models.CharField(verbose_name="手机号", max_length=11)
    state_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    rank = models.ForeignKey(RankModel, on_delete=models.CASCADE, verbose_name="职级")
    position = models.ForeignKey(PositionModel, on_delete=models.CASCADE, verbose_name="职位")
    address = models.CharField(verbose_name="地址", max_length=256)
    birthday = models.CharField(verbose_name="生日", null=True, max_length=10)
    sex = models.CharField(verbose_name="性别", max_length=10)
    # email = models.ForeignKey(EmaliModel,verbose_name="邮箱", on_delete=models.CASCADE, null=True)
    userstate = models.CharField(max_length=10, choices=STATE_OPTIONS, verbose_name="用户状态", default=3)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "user_tb"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# # 操作日志
class OperateModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.SET_NULL, verbose_name="操作者", null=True, blank=True)
    path = models.CharField(verbose_name="路径", max_length=256)
    dateline_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    action = models.CharField(verbose_name="登录动作", max_length=100)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "operate_tb"
        verbose_name = "操作日志"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.name


# 登录日志
class LoginLogModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.SET_NULL, verbose_name="登陆人", null=True, blank=True)
    address = models.TextField(verbose_name="登录地点", max_length=100)
    log_time = models.DateTimeField(verbose_name="登陆时间", auto_now_add=True)
    device = models.CharField(verbose_name="登陆设备", max_length=500)
    ip = models.CharField(verbose_name="登录ip", max_length=200)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)

    class Meta:
        db_table = "login_log_tb"
        verbose_name = "登录日志"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.name


# 菜单表
class MenuModel(models.Model):
    # 菜单类型 type
    name = models.CharField(verbose_name="菜单名称", max_length=25)
    menurl = models.CharField(verbose_name="菜单地址", max_length=256, null=True, blank=True)
    menuimg = models.CharField(verbose_name="菜单图标", max_length=256, null=True, blank=True)
    menu_id = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="外链的菜单id", null=True, blank=True)
    remark = models.CharField(verbose_name="备注", max_length=100, null=True, blank=True)
    is_delete = models.BooleanField(verbose_name="是否删除",default=False)
    position = models.IntegerField(verbose_name="职级", null=True, blank=True)

    class Meta:
        db_table = "menu_tb"
        verbose_name = "菜单表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name



